package com.cubeofnine.sugar.service;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.ksoap2.serialization.SoapObject;

import com.cubeofnine.sugar.service.element.Contact;
import com.cubeofnine.sugar.service.element.EntryListResult;
import com.cubeofnine.sugar.service.element.EntryResult;
import com.cubeofnine.sugar.service.element.EntryValue;
import com.cubeofnine.sugar.service.element.ErrorValue;
import com.cubeofnine.sugar.service.element.Field;
import com.cubeofnine.sugar.service.element.IdMod;
import com.cubeofnine.sugar.service.element.LoginResult;
import com.cubeofnine.sugar.service.element.ModuleFields;
import com.cubeofnine.sugar.service.element.NameValue;
import com.cubeofnine.sugar.service.element.RelationshipValue;
import com.cubeofnine.sugar.service.element.ReturnAddContactList;
import com.cubeofnine.sugar.service.element.ReturnExportContactList;
import com.cubeofnine.sugar.service.element.UserAuth;
import com.cubeofnine.sugar.service.element.RelationshipsResult;
import com.cubeofnine.utils.WebServiceAdapter;

import android.util.Log;

public class SugarCrmService {
	private WebServiceAdapter wsAdapter = null;

	private static final String WSDL_TARGET_NAMESPACE = "http://www.sugarcrm.com/sugarcrm";
	private static final String ENCODE_STYLE = "http://schemas.xmlsoap.org/soap/encoding/";
	private static final String SUGAR_CRM_VERSION = "6.4.2";
	private static final int TIME_OUT = 10000;

	private static final String LOGIN = "login";
	private static final String LOGOUT = "logout";
	private static final String IS_USER_ADMIN = "is_user_admin";
	private static final String GET_MODULE_FIELDS = "get_module_fields";
	private static final String GET_ENTRY_LIST = "get_entry_list";
	private static final String GET_ENTRY = "get_entry";
	private static final String GET_RELATIONSHIPS = "get_relationships";
	private static final String SET_RELATIONSHIP = "set_relationship";
	private static final String CREATE_CONTACT = "create_contact";
	private static final String CREATE_ACCOUNT = "create_account";
	private static final String GET_CONTACT_LIST = "get_contact_list";
	private static final String ADD_CONTACT_LIST = "add_contact_list";
	private static final String EXPORT_CONTACT_LIST = "export_contact_list";

	public static String getClassName() {
		return SugarCrmService.class.getName();
	}

	public SugarCrmService(String url) {
		Log.i(getClassName(), "SugarCrmService() : Start");
		Log.d(getClassName(), "url = " + url);

		wsAdapter = new WebServiceAdapter(url, WSDL_TARGET_NAMESPACE, TIME_OUT,
				false, ENCODE_STYLE);

		Log.i(getClassName(), "SugarCrmService() : End");
	}

	private Object getField(SoapObject result, String propertyName) {
		Object object = null;

		try {
			object = result.getProperty(propertyName);
		} catch (Exception e) {

		}

		return object;
	}

	private int getFieldInt(SoapObject result, String propertyName) {
		int object = -1;
		try {
			object = Integer.parseInt(getFieldString(result, propertyName), 10);
		} catch (Exception e) {

		}
		return object;
	}

	private String getFieldString(SoapObject result, String propertyName) {
		String object = null;
		try {
			object = String.valueOf(getField(result, propertyName));
		} catch (Exception e) {

		}
		return object;
	}

	private ErrorValue getErrorValue(String propertyName, SoapObject object) {
		ErrorValue result = new ErrorValue();

		try {
			SoapObject soapObject = (SoapObject) object
					.getProperty(propertyName);
			result.setNumber(getFieldInt(soapObject, "number"));
			result.setName(getFieldString(soapObject, "name"));
			result.setDescription(getFieldString(soapObject, "description"));
		} catch (Exception e) {

		}

		return result;
	}

	private NameValue getNameValue(SoapObject object) throws Exception {
		NameValue result = new NameValue();
		result.setName(getFieldString(object, "name"));
		result.setValue(getFieldString(object, "value"));
		return result;
	}

	private IdMod getIdMod(SoapObject soap) throws Exception {
		IdMod idMod = new IdMod();
		idMod.setId(getFieldString(soap, "id"));
		idMod.setDeleted(getFieldInt(soap, "deleted"));
		idMod.setDateModified(getFieldString(soap, "date_modified"));
		return idMod;
	}

	private Field getField(SoapObject soap) throws Exception {
		Field field = new Field();
		field.setName(getFieldString(soap, "name"));
		field.setType(getFieldString(soap, "type"));
		field.setRequired(getFieldInt(soap, "required"));

		try {
			field.setLabel(getFieldString(soap, "label"));
		} catch (Exception e) {

		}

		try {
			field.setDefaultValue(getFieldString(soap, "default_value"));
		} catch (Exception e) {

		}

		return field;
	}

	private EntryValue getEntryValue(SoapObject soap) throws Exception {
		EntryValue entryValue = new EntryValue();
		entryValue.setId(getFieldString(soap, "id"));
		entryValue.setModuleName(getFieldString(soap, "module_name"));
		return entryValue;
	}

	private ModuleFields getModuleFields(SoapObject result) throws Exception {
		Log.i(getClassName(), "getModuleFields() : Start");
		ModuleFields moduleFields = new ModuleFields();
		// module_name
		moduleFields.setModuleName(getFieldString(result, "module_name"));

		// module_fields
		try {
			Object moduleFieldsObject = getField(result, "module_fields");
			if (moduleFieldsObject != null) {
				List<?> moduleFieldsList = (Vector<?>) moduleFieldsObject;
				if (!moduleFieldsList.isEmpty()) {
					Iterator<?> moduleFieldsIter = moduleFieldsList.iterator();

					SoapObject soap = null;
					Field field = null;

					Object optionsObject = null;
					List<?> optionsList = null;
					Iterator<?> optionsIter = null;
					while (moduleFieldsIter.hasNext()) {
						soap = (SoapObject) moduleFieldsIter.next();
						field = getField(soap);

						optionsObject = getField(soap, "options");
						if (optionsObject != null) {
							optionsList = (Vector<?>) optionsObject;
							if (!optionsList.isEmpty()) {
								optionsIter = optionsList.iterator();
								while (optionsIter.hasNext()) {
									soap = (SoapObject) optionsIter.next();
									field.addOption(getNameValue(soap));
								}
							}
						}

						moduleFields.addModuleField(field);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getModuleFieldsResult()", e);
		}

		// error
		moduleFields.setError(getErrorValue("error", result));

		Log.i(getClassName(), "getModuleFields() : End");
		return moduleFields;
	}

	private EntryResult getEntry(SoapObject result) throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		EntryResult entryResult = new EntryResult();

		// field_list
		try {
			Object fieldObject = getField(result, "field_list");
			if (fieldObject != null) {
				List<?> fieldList = (Vector<?>) fieldObject;
				if (!fieldList.isEmpty()) {
					Iterator<?> fieldIter = fieldList.iterator();

					SoapObject soap = null;
					Field field = null;

					Object optionsObject = null;
					List<?> optionsList = null;
					Iterator<?> optionsIter = null;
					while (fieldIter.hasNext()) {
						soap = (SoapObject) fieldIter.next();
						field = getField(soap);

						optionsObject = getField(soap, "options");
						if (optionsObject != null) {
							optionsList = (Vector<?>) optionsObject;
							if (!optionsList.isEmpty()) {
								optionsIter = optionsList.iterator();
								while (optionsIter.hasNext()) {
									soap = (SoapObject) optionsIter.next();
									field.addOption(getNameValue(soap));
								}
							}
						}

						entryResult.addField(field.getName(), field);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getEntryList()", e);
		}

		// entry_list
		try {
			Object fieldObject = getField(result, "entry_list");
			if (fieldObject != null) {
				List<?> fieldList = (Vector<?>) fieldObject;
				if (!fieldList.isEmpty()) {
					Iterator<?> fieldIter = fieldList.iterator();

					SoapObject soap = null;
					EntryValue entryValue = null;

					Object objectsObject = null;
					List<?> objectsList = null;
					Iterator<?> objectsIter = null;
					NameValue nameValue = null;
					while (fieldIter.hasNext()) {
						entryValue = new EntryValue();
						soap = (SoapObject) fieldIter.next();
						entryValue = getEntryValue(soap);

						objectsObject = getField(soap, "name_value_list");
						if (objectsObject != null) {
							objectsList = (Vector<?>) objectsObject;
							if (!objectsList.isEmpty()) {
								objectsIter = objectsList.iterator();
								while (objectsIter.hasNext()) {
									soap = (SoapObject) objectsIter.next();
									nameValue = getNameValue(soap);
									entryValue.addNameValue(
											nameValue.getName(), nameValue);
								}
							}
						}

						entryResult.addEntry(entryValue.getId(), entryValue);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getEntryList()", e);
		}

		// error
		entryResult.setError(getErrorValue("error", result));

		Log.i(getClassName(), "getEntryList() : End");
		return entryResult;
	}

	private EntryListResult getEntryList(SoapObject result) throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		EntryListResult entryListResult = new EntryListResult();

		// result_count
		entryListResult.setResultCount(getFieldInt(result, "result_count"));
		Log.d(getClassName(),
				"result_count = " + entryListResult.getResultCount());

		// next_offset
		entryListResult.setNextOffset(getFieldInt(result, "next_offset"));
		Log.d(getClassName(),
				"next_offset = " + entryListResult.getNextOffset());

		// field_list
		try {
			Object fieldObject = getField(result, "field_list");
			if (fieldObject != null) {
				List<?> fieldList = (Vector<?>) fieldObject;
				if (!fieldList.isEmpty()) {
					Iterator<?> fieldIter = fieldList.iterator();
					SoapObject soap = null;
					Field field = null;

					Object optionsObject = null;
					List<?> optionsList = null;
					Iterator<?> optionsIter = null;
					while (fieldIter.hasNext()) {
						soap = (SoapObject) fieldIter.next();
						field = getField(soap);

						optionsObject = getField(soap, "options");
						if (optionsObject != null) {
							optionsList = (Vector<?>) optionsObject;
							if (!optionsList.isEmpty()) {
								optionsIter = optionsList.iterator();
								while (optionsIter.hasNext()) {
									soap = (SoapObject) optionsIter.next();
									field.addOption(getNameValue(soap));
								}
							}
						}

						entryListResult.addField(field.getName(), field);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getEntryList()", e);
		}

		// entry_list
		try {
			Object fieldObject = getField(result, "entry_list");
			if (fieldObject != null) {
				List<?> fieldList = (Vector<?>) fieldObject;
				if (!fieldList.isEmpty()) {
					Iterator<?> fieldIter = fieldList.iterator();
					SoapObject soap = null;
					EntryValue entryValue = null;

					Object optionsObject = null;
					List<?> objectsList = null;
					Iterator<?> optionsIter = null;
					NameValue nameValue = null;
					while (fieldIter.hasNext()) {
						soap = (SoapObject) fieldIter.next();
						entryValue = getEntryValue(soap);

						optionsObject = getField(soap, "name_value_list");
						if (optionsObject != null) {
							objectsList = (Vector<?>) optionsObject;
							if (!objectsList.isEmpty()) {
								optionsIter = objectsList.iterator();
								while (optionsIter.hasNext()) {
									soap = (SoapObject) optionsIter.next();
									nameValue = getNameValue(soap);

									entryValue.addNameValue(
											nameValue.getName(), nameValue);
								}
							}
						}

						entryListResult
								.addEntry(entryValue.getId(), entryValue);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getEntryList()", e);
		}

		// error
		entryListResult.setError(getErrorValue("error", result));

		Log.i(getClassName(), "getEntryList() : End");
		return entryListResult;
	}

	private RelationshipsResult getRelationships(SoapObject result)
			throws Exception {
		Log.i(getClassName(), "getRelationships() : Start");
		RelationshipsResult relationshipsResult = new RelationshipsResult();

		// ids
		try {
			Object fieldObject = getField(result, "ids");
			if (fieldObject != null) {
				List<?> fieldList = (Vector<?>) fieldObject;
				if (!fieldList.isEmpty()) {
					Iterator<?> fieldIter = fieldList.iterator();

					SoapObject soap = null;
					IdMod idMod = null;
					while (fieldIter.hasNext()) {
						soap = (SoapObject) fieldIter.next();
						idMod = getIdMod(soap);
						relationshipsResult.addId(idMod.getId(), idMod);
					}
				}
			}
		} catch (Exception e) {
			Log.e(getClassName(), "getEntryList()", e);
		}

		// error
		relationshipsResult.setError(getErrorValue("error", result));

		Log.i(getClassName(), "getRelationships() : End");
		return relationshipsResult;
	}

	public LoginResult getSessionId(String username, String password)
			throws Exception {
		Log.i(getClassName(), "getSessionId() : Start");
		Log.d(getClassName(), "username = " + username);
		Log.d(getClassName(), "password = " + password);
		Log.d(getClassName(), "sugarCrmVersion = " + SUGAR_CRM_VERSION);
		LoginResult loginResult = null;

		UserAuth userAuth = new UserAuth();
		userAuth.setUser_name(username);
		userAuth.setPassword(password);
		userAuth.setVersion(SUGAR_CRM_VERSION);

		wsAdapter.intiRequest(LOGIN);
		wsAdapter.setRequestProperty("user_auth", userAuth);
		wsAdapter.setRequestProperty("application_name", Class.class.getName());

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			SoapObject result = (SoapObject) response;

			loginResult = new LoginResult();
			loginResult.setId(result.getProperty("id").toString());
			loginResult.setErrorValue(getErrorValue("error", result));

			Log.d(getClassName(), "result = " + result);
		}

		Log.i(getClassName(), "getSessionId() : End");
		return loginResult;
	}

	public ErrorValue logout(String sessionId) throws Exception {
		Log.i(getClassName(), "logout() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		ErrorValue error = new ErrorValue();

		wsAdapter.intiRequest(LOGOUT);
		wsAdapter.setRequestProperty("session", sessionId);
		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			SoapObject result = (SoapObject) response;

			error = getErrorValue("error", result);

			Log.d(getClassName(), "result = " + result);
		}

		Log.i(getClassName(), "logout() : End");
		return error;
	}

	public int isUserAdmin(String sessionId) {
		Log.i(getClassName(), "isUserAdmin() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);

		int result = 0;
		try {
			wsAdapter.intiRequest(IS_USER_ADMIN);
			wsAdapter.setRequestProperty("session", sessionId);

			Log.d(getClassName(), "request = " + wsAdapter.getRequest());

			String str = "";
			Object response = wsAdapter.callService();
			if (response != null) {
				str = String.valueOf(response);
				if (str == null || str.isEmpty()) {
					str = "0";
				}
			} else {
				str = "0";
			}

			result = Integer.parseInt(str, 10);
		} catch (Exception e) {
			Log.e(getClassName(), "isUserAdmin()", e);
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "isUserAdmin() : End");
		return result;
	}

	public ModuleFields getModuleFields(String sessionId, String moduleName)
			throws Exception {
		Log.i(getClassName(), "getModuleFields() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		ModuleFields result = null;

		wsAdapter.intiRequest(GET_MODULE_FIELDS);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("module_name", moduleName);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			result = getModuleFields((SoapObject) response);
		}

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "getModuleFields() : End");
		return result;
	}

	public EntryListResult getEntryList(String sessionId, String moduleName,
			String query, String orderBy, int offset, Vector<String> fieldName,
			int maxResults, int deleted) throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		Log.d(getClassName(), "query = " + query);
		Log.d(getClassName(), "orderBy = " + orderBy);
		Log.d(getClassName(), "offset = " + offset);
		Log.d(getClassName(), "fieldName = " + fieldName);
		Log.d(getClassName(), "maxResults = " + maxResults);
		Log.d(getClassName(), "deleted = " + deleted);
		EntryListResult result = null;

		wsAdapter.intiRequest(GET_ENTRY_LIST);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("module_name", moduleName);
		wsAdapter.setRequestProperty("query", query);
		wsAdapter.setRequestProperty("order_by", orderBy);
		wsAdapter.setRequestProperty("offset", offset);
		wsAdapter.setRequestProperty("max_results", maxResults);
		wsAdapter.setRequestProperty("deleted", deleted);
		// wsAdapter.setRequestProperty("select_fields", fieldName);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();

		Log.d(getClassName(), "response = " + response);
		if (response != null) {
			result = getEntryList((SoapObject) response);
		}

		Log.i(getClassName(), "getEntryList() : End");
		return result;
	}

	public EntryResult getEntry(String sessionId, String moduleName, String id,
			Vector<String> fieldName) throws Exception {
		Log.i(getClassName(), "getEntry() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		Log.d(getClassName(), "id = " + id);
		Log.d(getClassName(), "fieldName = " + fieldName);
		EntryResult result = null;

		wsAdapter.intiRequest(GET_ENTRY);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("module_name", moduleName);
		wsAdapter.setRequestProperty("id", id);
		// wsAdapter.setRequestProperty("select_fields", fieldName);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			result = getEntry((SoapObject) response);
		}

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "getEntry() : End");
		return result;
	}

	public RelationshipsResult getRelationships(String sessionId,
			String moduleName, String moduleId, String relatedModule,
			String relatedModuleQuery, boolean isDeleted) throws Exception {
		Log.i(getClassName(), "getRelationships() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		Log.d(getClassName(), "moduleId = " + moduleId);
		Log.d(getClassName(), "relatedModule = " + relatedModule);
		Log.d(getClassName(), "relatedModuleQuery = " + relatedModuleQuery);
		Log.d(getClassName(), "deteted = " + isDeleted);
		RelationshipsResult result = null;

		int deteted = 0;
		if (isDeleted) {
			deteted = 1;
		}

		wsAdapter.intiRequest(GET_RELATIONSHIPS);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("module_name", moduleName);
		wsAdapter.setRequestProperty("module_id", moduleId);
		wsAdapter.setRequestProperty("related_module", relatedModule);
		wsAdapter
				.setRequestProperty("related_module_query", relatedModuleQuery);
		wsAdapter.setRequestProperty("deleted", deteted);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			result = getRelationships((SoapObject) response);
		}

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "getRelationships() : End");
		return result;
	}

	public String createContact(String userName, String password,
			String firstName, String lastName, String email) throws Exception {
		Log.i(getClassName(), "createContact() : Start");
		Log.d(getClassName(), "userName = " + userName);
		Log.d(getClassName(), "password = " + password);
		Log.d(getClassName(), "firstName = " + firstName);
		Log.d(getClassName(), "lastName = " + lastName);
		Log.d(getClassName(), "email = " + email);
		String result = "";

		wsAdapter.intiRequest(CREATE_CONTACT);
		wsAdapter.setRequestProperty("user_name", userName);
		wsAdapter.setRequestProperty("password", password);
		wsAdapter.setRequestProperty("first_name", firstName);
		wsAdapter.setRequestProperty("last_name", lastName);
		wsAdapter.setRequestProperty("email_address", email);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			result = String.valueOf(response);
		}

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "createContact() : End");
		return result;
	}

	public ErrorValue setRelationship(String sessionId, String moduleName,
			String moduleId, String relateModule, String relateId)
			throws Exception {
		Log.i(getClassName(), "createContact() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		Log.d(getClassName(), "moduleId = " + moduleId);
		Log.d(getClassName(), "relateModule = " + relateModule);
		Log.d(getClassName(), "relateId = " + relateId);

		RelationshipValue relationshipValue = new RelationshipValue();
		relationshipValue.setModule1(moduleName);
		relationshipValue.setModule1_id(moduleId);
		relationshipValue.setModule2(relateModule);
		relationshipValue.setModule2_id(relateId);

		wsAdapter.intiRequest(SET_RELATIONSHIP);
		wsAdapter.setRequestProperty("user_name", sessionId);
		wsAdapter.setRequestProperty("set_relationship_value",
				relationshipValue);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		SoapObject response = (SoapObject) wsAdapter.callService();
		ErrorValue error = getErrorValue("error", response);

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "createContact() : End");
		return error;
	}

	public String createAccount(String userName, String password, String name,
			String phone, String website) throws Exception {
		Log.i(getClassName(), "createContact() : Start");
		Log.d(getClassName(), "userName = " + userName);
		Log.d(getClassName(), "password = " + password);
		Log.d(getClassName(), "name = " + name);
		Log.d(getClassName(), "phone = " + phone);
		Log.d(getClassName(), "website = " + website);
		String result = "";

		wsAdapter.intiRequest(CREATE_ACCOUNT);
		wsAdapter.setRequestProperty("user_name", userName);
		wsAdapter.setRequestProperty("password", password);
		wsAdapter.setRequestProperty("name", name);
		wsAdapter.setRequestProperty("phone", phone);
		wsAdapter.setRequestProperty("website", website);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();
		if (response != null) {
			result = String.valueOf(response);
		}

		Log.d(getClassName(), "response = " + response);
		Log.i(getClassName(), "createContact() : End");
		return result;
	}

	public EntryListResult getContactList(String sessionId, String query,
			String orderBy, int offset, int maxResults) throws Exception {
		Log.i(getClassName(), "getContactList() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "query = " + query);
		Log.d(getClassName(), "orderBy = " + orderBy);
		Log.d(getClassName(), "offset = " + offset);
		Log.d(getClassName(), "maxResults = " + maxResults);
		EntryListResult result = null;

		wsAdapter.intiRequest(GET_CONTACT_LIST);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("query", query);
		wsAdapter.setRequestProperty("order_by", orderBy);
		wsAdapter.setRequestProperty("offset", offset);
		wsAdapter.setRequestProperty("max_results", maxResults);
		// wsAdapter.setRequestProperty("select_fields", null);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();

		Log.d(getClassName(), "response = " + response);
		if (response != null) {
			result = getEntryList((SoapObject) response);
		}

		Log.i(getClassName(), "getContactList() : End");
		return result;
	}

	public ReturnAddContactList addContactList(String sessionId,
			List<Contact> contacts) throws Exception {
		Log.i(getClassName(), "addContactList() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "contacts = " + contacts);
		ReturnAddContactList result = null;

		wsAdapter.intiRequest(ADD_CONTACT_LIST);
		wsAdapter.setRequestProperty("session", sessionId);
		wsAdapter.setRequestProperty("contacts", contacts);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();

		Log.d(getClassName(), "response = " + response);
		if (response != null) {
			result = new ReturnAddContactList((SoapObject) response);
		}

		Log.i(getClassName(), "addContactList() : End");
		return result;
	}

	public ReturnExportContactList exportContactList(String sessionId)
			throws Exception {
		Log.i(getClassName(), "exportContactList() : Start");
		Log.d(getClassName(), "sessionId = " + sessionId);
		ReturnExportContactList result = null;

		wsAdapter.intiRequest(EXPORT_CONTACT_LIST);
		wsAdapter.setRequestProperty("session", sessionId);

		Log.d(getClassName(), "request = " + wsAdapter.getRequest());

		Object response = wsAdapter.callService();

		Log.d(getClassName(), "response = " + response);
		if (response != null) {
			result = new ReturnExportContactList((SoapObject) response);
		}

		Log.i(getClassName(), "exportContactList() : End");
		return result;
	}
}